import { dirname, join as joinPath } from "path";
import { ModernVersion, ModernVersionFactory } from "./version";
import { BMCLAPIMirror, Downloader, Java } from "./download";
import {TaskDownloadAssetIndex, TaskDownloadAssets, TaskDownloadClientJar, TaskDownloadLibraries, TaskExtractNatives} from "./task/download";
import { TaskManager } from "./task";
import { copyFileSync } from "fs";
import { OfflineProfile } from "./profile";
import { TaskLaunchGame } from "./task/launch";
import { damnTest } from "./login/login";

const TEST_FILES = joinPath(__dirname, "../test");
const TEST_FAKE_GAME = joinPath(TEST_FILES, ".minecraft");
const TEST_CLIENT_JSON_1_18_2_ORIGINAL = joinPath(TEST_FILES, "1.18.2.json");
const TEST_CLIENT_JSON_1_18_2 = joinPath(TEST_FAKE_GAME, "versions", "1.18.2", "1.18.2.json");

function summary(clientJSONPath:string, gamePath:string):ModernVersion {
    const version = new ModernVersionFactory().create(clientJSONPath, gamePath);

    return version;
}

async function test() {
    Downloader.createFolder(dirname(TEST_CLIENT_JSON_1_18_2));
    copyFileSync(TEST_CLIENT_JSON_1_18_2_ORIGINAL, TEST_CLIENT_JSON_1_18_2);
    const v = summary(TEST_CLIENT_JSON_1_18_2, TEST_FAKE_GAME);
    const library = v.libraries[0];

    const java = new Java("C:\\Program Files\\Java\\jdk-22\\bin\\java.exe");

    const taskMgr = new TaskManager();

    Downloader.mirror = new BMCLAPIMirror();

    taskMgr.addTask(new TaskDownloadLibraries(v));
    taskMgr.addTask(new TaskExtractNatives(v));
    taskMgr.addTask(new TaskDownloadClientJar(v));
    taskMgr.addTask(new TaskDownloadAssetIndex(v));
    taskMgr.addTask(new TaskDownloadAssets(v));
    taskMgr.addTask(new TaskLaunchGame(v, java, new OfflineProfile("TunaFish2K"), {
        quickPlayRealms:null,
        quickPlayMultiplayer:null,
        quickPlaySinglePlayer:null,
        quickPlayPath:null,
        isQuickPlayRealms:null,
        isQuickPlayMultiPlayer:null,
        isQuickPlaySinglePlayer:null,
        hasCustomResolution:null,
        resolutionWidth:null,
        resolutionHeight:null,
        hasQuickPlaysSupport:null,
    }))

    await taskMgr.run();
}


async function cli() {
    Downloader.createFolder(joinPath(__dirname, "../test", "secrets"));
    switch (process.argv[2]) {
        case "damn":
            await damnTest(process.argv[3]);
            break;
        case "launch":
        default:
            test();
    }
}

cli().catch((err)=>{
    console.log(err);
});

